﻿using Sea;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using UeyesAIS.DataDefinition;

namespace UeyesAIS.DataDefinition
{
    [Serializable]
    [DefaultPropertyAttribute("检测组参数")]
    [Category("检测组参数")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [InspGroup("InspGroupParameter")]
    [PropertyGroup(0)]
    public class InspGroupParameter : IComparable
    {
        private InspParameter _sp;
        private List<int> _inspmethods;
        private List<ReservedParam> _reservedParam;
        private string _cname;
        private IgnoseArea _igAr;
        private MeanThresold _meanthr;
        public InspGroupParameter()
            : this("")
        {
        }
        public InspGroupParameter(string cName)
        {
            _cname = cName;
        }
        /// <summary>
        /// 1.算法参数
        /// </summary>
        [Description("图片参数")]
        [Category("算法的参数")]
        [Browsable(false)]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        [DisplayName("算法参数")]
        [PropertyGroup(0)]
        public InspParameter Sp
        {
            get
            {
                if (_sp == null)
                {
                    _sp = new InspParameter();
                }
                return _sp;
            }
            set
            {
                _sp = value;
            }
        }
        /// <summary>
        /// 2.检测方法索引
        /// </summary>
        [Browsable(true)]
        [Category("UserSetting")]
        [Description("检测方法")]
        [DisplayName("检测方法索引")]
        [PropertyGroup(4)]
        public List<int> InspMethods
        {
            get
            {
                if (_inspmethods == null)
                {
                    _inspmethods = new List<int>();
                }
                return _inspmethods;
            }
            set
            {
                _inspmethods = value;
            }
        }
        /// <summary>
        /// 3.曝光时间
        /// </summary>
        [Browsable(true)]
        [Category("UserSetting")]
        [Description("曝光时间")]
        [DisplayName("曝光时间")]
        [PropertyGroup(1)]
        [DefaultValue(1000)]
        public double ExproseTime { get; set; }
        /// <summary>
        /// 4.忽略区域
        /// </summary>
        [TypeConverter(typeof(ExpandableObjectConverter)), Browsable(false), Category("UserSetting"), Description("忽略区域"), DisplayName("忽略区域"), PropertyGroup(1)]
        public IgnoseArea IgAr
        {
            get
            {
                if (_igAr == null) _igAr = new IgnoseArea();
                return _igAr;
            }
            set
            {
                _igAr = value;
            }
        }
        /// <summary>
        /// 5.灰度阈值
        /// </summary>
        [TypeConverter(typeof(ExpandableObjectConverter)), Browsable(false), Category("UserSetting"), Description("灰度阈值"), DisplayName("灰度阈值"), PropertyGroup(1)]
        public MeanThresold MeanThr
        {
            get
            {
                if (_meanthr == null) _meanthr = new MeanThresold();
                return _meanthr;
            }
            set
            {
                _meanthr = value;
            }
        }
        /// <summary>
        /// 6.相机名称
        /// </summary>
        [Browsable(true)]
        [Category("UserSetting")]
        [Description("相机名称")]
        [DisplayName("相机名称")]
        [PropertyGroup(1)]
        [DefaultValue("")]
        public string CameraName { get { return _cname; } set { _cname = value; } }
        /// <summary>
        /// 7.算法预留参数
        /// </summary>
        [Browsable(true)]
        [Category("UserSetting")]
        [Description("预留参数组")]
        [DisplayName("预留参数组")]
        [PropertyGroup(1)]
        public List<ReservedParam> RParam
        {
            get
            {
                if (_reservedParam == null)
                {
                    _reservedParam = new List<ReservedParam>();
                }
                return _reservedParam;
            }
            set
            {
                _reservedParam = value;
            }
        }

        public InspGroupParameter Clone()
        {
            InspGroupParameter pic = new InspGroupParameter();
            pic.CameraName = CameraName;
            pic.Sp = Sp.Clone();
            pic.ExproseTime = ExproseTime;
            pic.IgAr = IgAr.Clone();
            InspMethods.ForEach(x => pic.InspMethods.Add(x));
            pic.MeanThr = MeanThr.Clone();
            RParam.ForEach(x => pic.RParam.Add(x.Clone()));
            return pic;
        }
        #region 实现Icompare
        public int CompareTo(object obj)
        {
            if (obj is InspGroupParameter)
            {
                var temp = obj as InspGroupParameter;
                return Compare(temp.CameraName, CameraName);
                //if (temp.CameraName > CameraName)
                //    return -1;
                //else
                //    return 1;
            }
            else
                return 0;
        }
        private int Compare(Object x, Object y)
        {
            if (x == null || y == null)
                throw new ArgumentException("Parameters can't be null");
            string fileA = x as string;
            string fileB = y as string;
            char[] arr1 = fileA.ToCharArray();
            char[] arr2 = fileB.ToCharArray();
            int i = 0, j = 0;
            while (i < arr1.Length && j < arr2.Length)
            {
                if (char.IsDigit(arr1[i]) && char.IsDigit(arr2[j]))
                {
                    string s1 = "", s2 = "";
                    while (i < arr1.Length && char.IsDigit(arr1[i]))
                    {
                        s1 += arr1[i];
                        i++;
                    }
                    while (j < arr2.Length && char.IsDigit(arr2[j]))
                    {
                        s2 += arr2[j];
                        j++;
                    }
                    if (int.Parse(s1) > int.Parse(s2))
                    {
                        return 1;
                    }
                    if (int.Parse(s1) < int.Parse(s2))
                    {
                        return -1;
                    }
                }
                else
                {
                    if (arr1[i] > arr2[j])
                    {
                        return 1;
                    }
                    if (arr1[i] < arr2[j])
                    {
                        return -1;
                    }
                    i++;
                    j++;
                }
            }
            if (arr1.Length == arr2.Length)
            {
                return 0;
            }
            else
            {
                return arr1.Length > arr2.Length ? 1 : -1;
            }
        }
        #endregion 实现Icompare
        public override string ToString()
        {
            return string.Format("{0}参数", CameraName);
        }
    }
    [Serializable]
    [DefaultPropertyAttribute("基本图片参数")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [InspGroup("BasePicParameter")]
    public class BasePicParameter : IComparable
    {

        private int _changcolor = 1500;
        private int _intensity = 255;
        public BasePicParameter()
            : this("", 0)
        {

        }
        public BasePicParameter(string picn, int index)
        {
            Picname = picn;
            Picindex = index;
        }

        /// <summary>
        /// 1.图片名称
        /// </summary>
        [Browsable(true)]
        [Category("UserSetting")]
        [Description("图片名称")]
        [DisplayName("图片名称")]
        [PropertyGroup(1)]
        public string Picname { get; set; }
        /// <summary>
        /// 2.图片索引
        /// </summary>
        [Browsable(true)]
        [Category("UserSetting")]
        [Description("图片索引")]
        [DisplayName("图片索引")]
        [PropertyGroup(1)]
        public int Picindex { get; set; }
        /// <summary>
        /// 3.切图延时
        /// </summary>
        [Browsable(true), Category("UserSetting"), Description("拍图前延时(ms)"), DisplayName("切图延时"), PropertyGroup(1), DefaultValue(400)]
        public int ChangeColorTime
        { get { return _changcolor; } set { _changcolor = value; } }
        /// <summary>
        /// 4.拍图延时
        /// </summary>
        [Browsable(true), Category("UserSetting"), Description("拍图最长等待时间(ms)"), DisplayName("拍图延时"), PropertyGroup(1), DefaultValue(400)]
        public int DelayTime { get; set; }
        /// <summary>
        /// 4.光源亮度
        /// </summary>
        [Browsable(true), Category("UserSetting"), Description("光源亮度(0-255)"), DisplayName("光源亮度"), PropertyGroup(1), DefaultValue(255)]
        public int Intensity
        { get { return _intensity; } set { _intensity = value; } }

        /// <summary>
        /// 5.切图索引
        /// </summary>
        [Browsable(true)]
        [Category("UserSetting")]
        [Description("ETS点图索引")]
        [DisplayName("ETS点图索引")]
        [PropertyGroup(1)]
        public int ETSPicIndex { get; set; }
        public virtual BasePicParameter clone(string picname, int index)
        {
            BasePicParameter pic = new BasePicParameter(picname, index);
            pic.ChangeColorTime = ChangeColorTime;
            pic.DelayTime = DelayTime;
            pic.Intensity = Intensity;
            pic.ETSPicIndex = ETSPicIndex;
            return pic;
        }
        public virtual BasePicParameter clone()
        {
            BasePicParameter pic = new BasePicParameter(Picname, Picindex);
            pic.Picname = Picname;
            pic.Picindex = Picindex;
            pic.ChangeColorTime = ChangeColorTime;
            pic.DelayTime = DelayTime;
            pic.Intensity = Intensity;
            pic.ETSPicIndex = ETSPicIndex;
            return pic;
        }
        public int CompareTo(object obj)
        {
            if (obj is BasePicParameter)
            {
                var temp = obj as BasePicParameter;
                if (temp.Picindex > this.Picindex)
                    return -1;
                else
                    return 1;
            }
            else
                return 0;
        }
        public override string ToString()
        {
            return Picname + "基本参数";
        }
    }

    [Serializable]
    [DefaultPropertyAttribute("检测图片参数")]
    [Category("检测图片参数")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [InspGroup("AlgParameter")]
    [PropertyGroup(0)]
    public class AlgParameter : IComparable
    {
        private InspParameter _sp;
        private List<int> _inspmethods;
        private BasePicParameter _picParameter;
        private List<InspGroupParameter> _inspGroup;
        private string picname;
        private int picindex;
        public AlgParameter()
            : this("", 0)
        { }
        public AlgParameter(string picn, int index)
        {
            picname = picn;
            picindex = index;
        }
        /// <summary>
        /// 1.检测组参数
        /// </summary>
        [Browsable(false), Description("检测组参数"), Category("检测组参数"), DisplayName("检测组参数")]// 
        public List<InspGroupParameter> InspGroup
        {
            get
            {
                if (_inspGroup == null)
                {
                    _inspGroup = new List<InspGroupParameter>();
                }
                return _inspGroup;
            }
            set
            {
                _inspGroup = value;
            }
        }
        /// <summary>
        /// 2.图片基本参数
        /// </summary>
        [Browsable(true)]
        [Category("UserSetting")]
        [Description("图片基本参数")]
        [DisplayName("图片基本参数")]
        [PropertyGroup(4)]
        [TypeConverter(typeof(ExpandableObjectConverter))]
        public BasePicParameter PicParameter
        {
            get
            {
                if (_picParameter == null)
                {
                    _picParameter = new BasePicParameter(picname, picindex);
                }
                return _picParameter;
            }
            set
            {
                _picParameter = value;
            }
        }

        public AlgParameter clone(string name, int index)
        {
            AlgParameter spa = new AlgParameter(name, index + 1);
            spa.PicParameter = PicParameter.clone(name, index + 1);
            InspGroup.ForEach(x=> spa.InspGroup.Add(x.Clone()));
            return spa;
        }

        public AlgParameter clone()
        {
            AlgParameter spa = new AlgParameter();
            spa.PicParameter = PicParameter.clone();
            InspGroup.ForEach(x => spa.InspGroup.Add(x.Clone()));
            return spa;
        }

        public override string ToString()
        {
            return _picParameter.Picname + "检测图片参数";
        }
        public int CompareTo(object obj)
        {
            if (obj is AlgParameter)
            {
                var temp = obj as AlgParameter;
                if (temp.PicParameter.Picindex > this.PicParameter.Picindex)
                    return -1;
                else
                    return 1;
            }
            else
                return 0;
        }
    }

    [Serializable]
    [DefaultPropertyAttribute("API 光源参数")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [InspGroup("LightsIntensity")]
    public class LightsInten
    {
        public LightsInten()
        { }
        [Browsable(true), Category("UserSetting"), Description("上侧光亮度"), DisplayName("上侧光亮度"), PropertyGroup(4), DefaultValue(255)]

        public byte SideLightUp { get; set; }
        [Browsable(true), Category("UserSetting"), Description("下侧光亮度"), DisplayName("下侧光光亮度"), PropertyGroup(4), DefaultValue(255)]

        public byte SideLightDown { get; set; }
        [Browsable(true), Category("UserSetting"), Description("背光亮度"), DisplayName("背光亮度"), PropertyGroup(4), DefaultValue(255)]

        public byte BackLight { get; set; }
        public override string ToString()
        {
            return string.Format("{0},{1},{2}", SideLightUp, SideLightDown, BackLight);
        }
    }
    [Serializable]
    [DefaultPropertyAttribute("API 边缘屏蔽")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [InspGroup("IgnoseArea")]
    public class IgnoseArea
    {
        public IgnoseArea()
        { }
        [Browsable(true), Category("UserSetting"), Description("上边缘屏蔽"), DisplayName("上边缘屏蔽"), PropertyGroup(4), DefaultValue(0)]
        public int Upedge { get; set; }
        [Browsable(true), Category("UserSetting"), Description("下边缘屏蔽"), DisplayName("下边缘屏蔽"), PropertyGroup(4), DefaultValue(0)]
        public int Downedge { get; set; }
        [Browsable(true), Category("UserSetting"), Description("左边缘屏蔽"), DisplayName("左边缘屏蔽"), PropertyGroup(4), DefaultValue(0)]
        public int Leftedge { get; set; }
        [Browsable(true), Category("UserSetting"), Description("右边缘屏蔽"), DisplayName("右边缘屏蔽"), PropertyGroup(4), DefaultValue(0)]
        public int Rightedge { get; set; }
        [Browsable(true), Category("UserSetting"), Description("启用边缘屏蔽"), DisplayName("启用边缘屏蔽"), PropertyGroup(2), DefaultValue(0)]
        public bool IsOpen { get; set; }
        public override string ToString()
        {
            return string.Format("{0}启用边缘屏蔽", IsOpen ? "" : "不");
        }
        public IgnoseArea Clone()
        {
            IgnoseArea ig = new IgnoseArea();
            ig.IsOpen = this.IsOpen;
            ig.Upedge = this.Upedge;
            ig.Downedge = this.Downedge;
            ig.Leftedge = this.Leftedge;
            ig.Rightedge = this.Rightedge;
            return ig;
        }
    }
    [Serializable]
    [DefaultPropertyAttribute("API 延时参数")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [InspGroup("DelayTime")]
    public class DelayTime
    {
        public DelayTime()
        { }
        [Browsable(true), Category("UserSetting"), Description("校正延时"), DisplayName("校正延时"), PropertyGroup(2), DefaultValue(300)]
        public int Correct { get; set; }
        [Browsable(true), Category("UserSetting"), Description("切图延时"), DisplayName("切图延时"), PropertyGroup(2), DefaultValue(300)]
        public int ChangePic { get; set; }
        [Browsable(true), Category("UserSetting"), Description("电目测结果判读延时*100ms"), DisplayName("电目测结果延时"), PropertyGroup(2), DefaultValue(300)]
        public int AllPics { get; set; }
        [Browsable(true), Category("UserSetting"), Description("总结果判读延时*100ms"), DisplayName("总结果判读延时"), PropertyGroup(2), DefaultValue(300)]
        public int AllResults { get; set; }
        public override string ToString()
        {
            return string.Format("{0},{1},{2},{3}", Correct, ChangePic, AllPics, AllResults);
        }
    }
    [Serializable]
    [DefaultPropertyAttribute("灰度阈值")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [InspGroup("MeanThresold")]
    public class MeanThresold
    {
        private int _min = 0;
        private int _max = 255;
        public MeanThresold()
        { }
        [Browsable(true), Category("UserSetting"), Description("灰度最小值"), DisplayName("灰度最小值"), PropertyGroup(2), DefaultValue(300)]
        public int MeanMin
        {
            get { return _min; }
            set { _min = value; }
        }
        [Browsable(true), Category("UserSetting"), Description("灰度最大值"), DisplayName("灰度最大值"), PropertyGroup(2), DefaultValue(300)]
        public int MeanMax
        {
            get { return _max; }
            set { _max = value; }
        }
        public MeanThresold Clone()
        {
            MeanThresold mt = new MeanThresold();
            mt.MeanMax = this.MeanMax;
            mt.MeanMin = this.MeanMin;
            return mt;
        }

        public override string ToString()
        {
            return string.Format("最小:{0}，最大:{1}", MeanMin, MeanMax);
        }
    }
    [Serializable]
    [DefaultPropertyAttribute("预留参数")]
    [TypeConverter(typeof(ExpandableObjectConverter))]
    [InspGroup("ReservedParam")]
    public class ReservedParam
    {
        private double _value = 0;
        private string _describe = "预留参数";
        public ReservedParam()
        { }
        [Browsable(true), Category("UserSetting"), Description("值"), DisplayName("值"), PropertyGroup(2), DefaultValue(300)]
        public double ParamValue
        {
            get { return _value; }
            set { _value = value; }
        }
        [Browsable(true), Category("UserSetting"), Description("描述"), DisplayName("描述"), PropertyGroup(2), DefaultValue(300)]
        public string ParamDescrition
        {
            get { return _describe; }
            set { _describe = value; }
        }
        public ReservedParam Clone()
        {
            ReservedParam rp = new ReservedParam();
            rp.ParamValue = ParamValue;
            rp.ParamDescrition = ParamDescrition;
            return rp;
        }

        public override string ToString()
        {
            return string.Format("{0}", ParamDescrition ?? "描述");
        }
    }
}
